<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>校易淘 - 我的商品</title>
  <link rel="stylesheet" href="css/style.css">
  <script src="js/main.js" defer></script>
</head>
<body>

  <!-- 头部 -->
  <header>
    <h1>校易淘二手交易平台</h1>
    <nav>
      <a href="index.html">首页</a>
      <a href="cart.html">购物车</a>
      <a href="publish.html">发布商品</a>
      <a href="myitems.html">我的商品</a>
      <a href="orders.html">我的订单</a>
      <a href="login.html">登录</a>
      <a href="register.html">注册</a>
    </nav>
  </header>

  <div class="container">
    <h2 style="text-align:center; margin-bottom:20px;">我的商品</h2>

    <div id="my-items-list" class="product-list">
      <!-- 卖家商品卡片由 localStorage 渲染 -->
    </div>
  </div>

  <script>
    function renderMyItems() {
      const myItemsList = document.getElementById("my-items-list");
      myItemsList.innerHTML = "";

      let myItems = JSON.parse(localStorage.getItem("myItems")) || [];

      myItems.forEach((item, index) => {
        const card = document.createElement("div");
        card.className = "product-card";
        card.innerHTML = `
          <img src="${item.img}" alt="${item.title}">
          <h3>${item.title}</h3>
          <p>￥${item.price}</p>
          <p>${item.desc}</p>
          <p>联系方式：${item.contact}</p>
          <button onclick="removeMyItem(${index})">下架</button>
        `;
        myItemsList.appendChild(card);
      });
    }

    function removeMyItem(index) {
      let myItems = JSON.parse(localStorage.getItem("myItems")) || [];
      myItems.splice(index, 1);
      localStorage.setItem("myItems", JSON.stringify(myItems));
      renderMyItems();
    }

    document.addEventListener("DOMContentLoaded", renderMyItems);
  </script>

</body>
</html>
